package cn.wolfcode.wms.service.impl;

import cn.wolfcode.wms.domain.Systemmenu;
import cn.wolfcode.wms.mapper.SystemmenuMapper;
import cn.wolfcode.wms.qo.MenuQueryObject;
import cn.wolfcode.wms.service.ISystemmenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@Service
public class SystemmenuServiceImpl implements ISystemmenuService {
    @Autowired
    private SystemmenuMapper systemmenuMapper;

    @Override
    public void saveOrUpdate(Systemmenu entity) {
        if (entity.getId() != null) {
            systemmenuMapper.updateByPrimaryKey(entity);
        } else {
            systemmenuMapper.insert(entity);
        }
    }

    @Override
    //通过sn查询菜单
    public List<Map<String,Object>> getMenusBySn(String sn) {
        List<Map<String,Object> >  menus= systemmenuMapper.getMenusBySn(sn);
        return menus;
    }

    @Override
    public List<Systemmenu> queryAll() {
        List<Systemmenu> menus =  systemmenuMapper.queryAll();
        return menus;
    }

    @Override
    //面包屑 通过id查找所有的父菜单爷爷菜单
    public List<Systemmenu> selectParentMenusByParentId(Long parentId) {
        //如果父菜单id不为空应查询他的名字和id
        List<Systemmenu> menus = new ArrayList<>();
        if (parentId != null) {
            Systemmenu menu = systemmenuMapper.selectByPrimaryKey(parentId);

            menus.add(menu);
            while (menu.getParent()!= null) {
               menu = systemmenuMapper.selectByPrimaryKey(menu.getParent().getId());
                menus.add(menu);
            }
        }
        Collections.reverse(menus);

        return menus;
    }

    @Override
    public void delete(Long id) {
        if (id != null) {
            systemmenuMapper.deleteByParentId(id);
            systemmenuMapper.deleteByPrimaryKey(id);
        }
    }

    @Override
    public Systemmenu get(Long id) {

            Systemmenu entity = systemmenuMapper.selectByPrimaryKey(id);
        return entity;

    }

    @Override
    public List<Systemmenu> selectall(MenuQueryObject id) {
        List<Systemmenu> depts = systemmenuMapper.selectAll(id);
        return depts;
    }

    @Override
    //通过父id查他名字
    public Systemmenu selectByParentId(Long parentId) {
        Systemmenu systemmenu = systemmenuMapper.selectByParentId(parentId);
        return systemmenu;
    }


}
